﻿using System;
using System.Windows.Forms;
namespace AppFabricAdministrator
{
    public partial class AppfabricAdministrator : Form
    {

        /// <summary>
        /// currentInstance
        /// </summary>
        static AppfabricAdministrator currentInstance;

        /// <summary>
        /// Gets the current.
        /// </summary>
        public static AppfabricAdministrator Current
        {
            get
            {
                if (currentInstance == null)
                    currentInstance = new AppfabricAdministrator();
                return currentInstance;
            }
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="AppfabricAdministrator"/> class from being created.
        /// </summary>
        private AppfabricAdministrator()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Set appfabric RunSpace
        /// </summary>
        private void ImportDistributedCacheAdministrationModule()
        {
            string scriptText = @"Import-Module DistributedCacheAdministration;Use-CacheCluster";
            RunScript(scriptText, "Successfully Imported Module DistributedCacheAdministration");
        }

        /// <summary>
        /// RunScript
        /// </summary>
        /// <param name="scriptText"></param>
        public void RunScript(string scriptText, string commandExecutedSuccessText)
        {
            bool isSuccess = false;
            var result = ScriptHelper.RunCommandScript(scriptText, commandExecutedSuccessText, ScriptHelper.ResultToString, out isSuccess);
        }

        public void RefreshForm()
        {
            currentInstance = new AppfabricAdministrator();
            ImportDistributedCacheAdministrationModule();
        }

        #region events
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AppfabricAdministrator_Load(object sender, EventArgs e)
        {
            ImportDistributedCacheAdministrationModule();
        }

        /// <summary>
        /// Close the namespace when form closed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormPowerShellSample_FormClosing(object sender, FormClosingEventArgs e)
        {
            ScriptHelper.CurrentRunSpace.Close();
        }

        /// <summary>
        /// Handles the Click event of the btnRunscript control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnRunscript_Click(object sender, EventArgs e)
        {
            var selectedText = tbScript.SelectedText;
            var commandText = string.IsNullOrWhiteSpace(selectedText) ? tbScript.Text : selectedText;
            if (!string.IsNullOrWhiteSpace(commandText))
            {
                RunScript(commandText.Trim(), "Script executed successfully.");
            }
        }

        private void btnNewCache_Click(object sender, EventArgs e)
        {
            var newC = new NewCache();
            newC.Dock = DockStyle.Fill;
            this.scAdminMenu.Panel2.Controls.Clear();
            this.scAdminMenu.Panel2.Controls.Add(newC);
        }

        private void btnClearCache_Click(object sender, EventArgs e)
        {
            RemoveCache ec = new RemoveCache();
            ec.Dock = DockStyle.Fill;
            this.scAdminMenu.Panel2.Controls.Clear();
            this.scAdminMenu.Panel2.Controls.Add(ec);
        }

        private void btnRefreshCluster_Click(object sender, EventArgs e)
        {
            RefreshCache rc = new RefreshCache();
            rc.Dock = DockStyle.Fill;
            this.scAdminMenu.Panel2.Controls.Clear();
            this.scAdminMenu.Panel2.Controls.Add(rc);
        }

        private void btnCacheDetails_Click(object sender, EventArgs e)
        {
            CacheDetails cd = new CacheDetails();
            cd.Dock = DockStyle.Fill;
            this.scAdminMenu.Panel2.Controls.Clear();
            this.scAdminMenu.Panel2.Controls.Add(cd);
        }

        private void btnTestAdd_Click(object sender, EventArgs e)
        {
            TestCache tc = TestCache.Current;
            tc.Dock = DockStyle.Fill;
            this.tab2_ScTest.Panel2.Controls.Clear();
            this.tab2_ScTest.Panel2.Controls.Add(tc);
            tc.TestAdd();
        }

        private void btnTestGet_Click(object sender, EventArgs e)
        {
            TestCache tc = TestCache.Current;
            tc.Dock = DockStyle.Fill;
            this.tab2_ScTest.Panel2.Controls.Clear();
            this.tab2_ScTest.Panel2.Controls.Add(tc);
            tc.TestGet();
        }

        private void btnTestDelete_Click(object sender, EventArgs e)
        {
            TestCache tc = TestCache.Current;
            tc.Dock = DockStyle.Fill;
            this.tab2_ScTest.Panel2.Controls.Clear();
            this.tab2_ScTest.Panel2.Controls.Add(tc);
            tc.TestRemove();
        }

        private void btnCacheConfig_Click(object sender, EventArgs e)
        {
            this.adminTab_SC_Main.Panel2.Controls.Clear();
            CacheConfigUI cc = new CacheConfigUI();
            cc.Dock = DockStyle.Fill;
            this.adminTab_SC_Main.Panel2.Controls.Add(cc);
        }

        private void btnPermission_Click(object sender, EventArgs e)
        {
            this.adminTab_SC_Main.Panel2.Controls.Clear();
            Permission ps = new Permission();
            ps.Dock = DockStyle.Fill;
            this.adminTab_SC_Main.Panel2.Controls.Add(ps);
        }
        #endregion

        private static void WriteScriptToLog(string scriptText)
        {
            string text = string.Format(@"
DateTime: {0}
Script:   {1}
", System.DateTime.Now, scriptText);
           // AppfabricAdministrator.Current.tblog.Text += text;

        }
        public static void WriteScriptOut(string textout)
        {
            if (string.IsNullOrWhiteSpace(textout))
                return;
            string textTowrite = @"{0}
=====================================================================================
";
            AppfabricAdministrator.Current.tbScriptOut.Text += string.Format(textTowrite, textout.Trim());
            // Following line auto scroll the text box to end
            AppfabricAdministrator.Current.tbScriptOut.SelectionStart = AppfabricAdministrator.Current.tbScriptOut.Text.Length;
            AppfabricAdministrator.Current.tbScriptOut.ScrollToCaret();
        }
    }
}
